package com.pcbly.web.components.form;

import java.util.ArrayList;
import java.util.List;

import org.apache.oro.text.regex.MatchResult;
import org.apache.oro.text.regex.Pattern;
import org.apache.oro.text.regex.PatternCompiler;
import org.apache.oro.text.regex.PatternMatcher;
import org.apache.oro.text.regex.PatternMatcherInput;
import org.apache.oro.text.regex.Perl5Compiler;
import org.apache.oro.text.regex.Perl5Matcher;
 

public class HtmlElementComponents {
	private String element;

	public String getElement() {
		return element;
	}

	public void setElement(String element) {
		this.element = element;
	}

	protected List<String> patternRegStringToArray(String reg) {
		try {
			PatternCompiler compiler = new Perl5Compiler();
			PatternMatcher matcher = new Perl5Matcher();
			Pattern pattern = compiler.compile(reg);
			List<String> list = new ArrayList<String>();
			PatternMatcherInput input = new PatternMatcherInput(element);
			while (matcher.contains(input, pattern)) {
				MatchResult result = matcher.getMatch();
//				System.out.println(result.group(0));
//				System.out.println(result.group(1));
				list.add(result.group(0));
			}
			return list;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	private List<String> getInputStringToArray() {
		String reg = "<input[^>]*>";
		List<String> list = patternRegStringToArray(reg);
		return list;
	}

	private List<String> getTextAreaStringToArray() {
		String reg = "<textarea[^</]*</textarea>";
		List<String> list = patternRegStringToArray(reg);
		return list;
	}

	private List<String> getSelectStringToArray() {
		String reg = "<select[^>]*>(.*?)</select>";
		List<String> list = patternRegStringToArray(reg);
		return list;
	}

	public List<String> indexHtml() {
		List<String> list = new ArrayList<String>();
		list.addAll(getInputStringToArray());
//		System.out.println("split............."); 
		list.addAll(getTextAreaStringToArray());
//		System.out.println("split............."); 
		list.addAll(getSelectStringToArray());
		return list;
	}
	public static void main(String[] args){
		System.out.println("begin.............");
		HtmlElementComponents html=new HtmlElementComponents();
		
		String content="<form name=\"form\" method=\"post\"><table width=\"600\" cellspacing=\"1\" cellpadding=\"1\" border=\"0\" align=\"center\"><tbody><tr><td>客户名称：</td><td><input type=\"text\" name=\"402881e8254f7a8401254f807cd700f0\" table=\"4028ffbc24b3d07a0124b3d2aa640032\" size=\"10\" /></td></tr><tr><td>客户代码：</td><td><input type=\"text\" name=\"402881e8254f7a8401254f807cd700f1\" table=\"4028ffbc24b3d07a0124b3d2aa640032\" /></td></tr><tr><td>客户备注：</td><td><textarea name=\"402881e8254f7a8401254f807ce7010c\" table=\"4028ffbc24b3d07a0124b3d2aa640032\"></textarea></td></tr><tr><td>&nbsp;客户等级：</td><td>&nbsp;<select name=\"402881e8254f7a8401254f807cd700f2\" table=\"4028ffbc24b3d07a0124b3d2aa640032\" datasrc=\"SELECT b.id,b.name_ FROM sys_dict_comb a LEFT JOIN sys_dict_value bON a.dictvalueId=b.id WHERE a.dictid='4028ffbc24c736d10124c7772b6f000c'\" datafld=\"SYS_LIST_SQL\"><option selected=\"selected\" value=\"{宏控件}\">{宏控件}</option></select></td></tr><tr><td>&nbsp;</td><td>&nbsp;</td></tr><tr><td>&nbsp;</td><td>&nbsp;</td></tr></tbody></table></form><p>&nbsp;</p>";
		html.setElement(content);
		html.indexHtml();  
		System.out.println("end.............");
	}
}
